package com.hhkj.api.student.controller;

import com.hhkj.admin.student.domain.Student;
import com.hhkj.api.student.service.biz.ApiStudentBizService;
import com.ruoyi.framework.login.api.ApiLogin;
import com.ruoyi.common.core.domain.AjaxResult;
import com.hhkj.api.common.session.holder.model.LoginStudent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 学员信息Controller
 *
 * @author ruoyi
 * @since 2023-05-12
 */
@RestController
@RequestMapping("/api/student")
public class ApiStudentController {
    @Autowired
    private ApiStudentBizService apiStudentBizService;

    /** 获取当前登录学员信息 */
    @ApiLogin(LoginStudent.class)
    @GetMapping("")
    public AjaxResult get() {
        return AjaxResult.success(apiStudentBizService.get());
    }

    /**
     * 学员科目列表
     */
    @PostMapping("/course/list")
    public AjaxResult listCourse(@RequestBody Student studentReq) {
        return AjaxResult.success(apiStudentBizService.listCourse(studentReq));
    }

    /**
     * 学员登录
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody Student studentReq) {
        LoginStudent student = apiStudentBizService.login(studentReq);
        return AjaxResult.success(student);
    }

    /**
     * 学员登录 不传科目参数
     */
    @PostMapping("/new/login")
    public AjaxResult loginNew(@RequestBody Student studentReq) {
        LoginStudent student = apiStudentBizService.loginNew(studentReq);
        return AjaxResult.success(student);
    }

    /**
     * 学员退出登录
     */
    @PostMapping("/logout")
    public AjaxResult logout(@RequestBody LoginStudent studentReq) {
        return apiStudentBizService.logout(studentReq) ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 学员登录
     */
    @PostMapping("/register")
    public AjaxResult register(@RequestBody Student studentReq) {
        boolean isSuccess = apiStudentBizService.saveStudentAndCourse(studentReq);
        String message = isSuccess ? "操作成功" : "操作失败";
        return AjaxResult.success(message);
    }

    /**
     * 学员科目列表
     */
    @GetMapping("/course/free/list/{id}")
    public AjaxResult listFreeCourse(@PathVariable("id") Long id) {
        return AjaxResult.success(apiStudentBizService.listFreeCourseByParentId(id));
    }

}
